package com.czh.springboot04.service.serviceimpl;

import com.czh.springboot04.entity.SystemUser;
import com.czh.springboot04.mapper.SystemUserMapper;
import com.czh.springboot04.service.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SystemUserServiceImpl implements SystemUserService {
    @Autowired
    private SystemUserMapper systemUserMapper;

    @Override
    public SystemUser login(SystemUser systemUser) {

        return systemUserMapper.findByUserNameAndPassword(systemUser);
    }

    @Override
    public int add(SystemUser systemUser) {
        return systemUserMapper.insertSystemUser(systemUser);
    }

    @Override
    public SystemUser findById(Integer userId) {
        return systemUserMapper.selectByUserId(userId);
    }

    @Override
    public List<SystemUser> findAll() {
        return systemUserMapper.selectAll(null);
    }

    @Override
    public List<SystemUser> findByRole(int role) {
        return systemUserMapper.selectByRole(role);
    }

    @Override
    public SystemUser findByUsername(SystemUser systemUser) {
        return systemUserMapper.selectByUserName(systemUser);
    }

    @Override
    public int addSu(SystemUser systemUser) {
        return systemUserMapper.insertSu(systemUser);
    }

    @Override
    public List<HashMap<String, Object>> selectCount() {
        List<HashMap<Integer, Object>> list = systemUserMapper.selectCount();
        HashMap<String, Object> map = new HashMap<>();
        ////System.out.println(list);
        List list1 =  new ArrayList();
        int i = 0;
        if (list != null && !list.isEmpty()) {
            //System.out.println(list);
            for (HashMap<Integer, Object> hashMap : list) {
                Integer key = 0;
                String name = "";
                Integer value = 0;
                for (Map.Entry<Integer, Object> entry : hashMap.entrySet()) {
                    if(i==0) {
                        map = new HashMap<>();
                    }
                    i++;
                    //System.out.println(entry.getKey());
                    if ("name".equals(entry.getKey())) {
                        key = (Integer) entry.getValue();
                        if(key==1){
                            name = "管理员";
                        }else if(key==2){
                            name = "咨询师";
                        }else if(key==3) {
                            name = "客服";
                        }
                        map.put("name", name);
                    } else if ("value".equals(entry.getKey())) {
                        //我需要的是int型所以做了如下转换，实际上返回的object应为Long。
                        value =  ((Long)entry.getValue()).intValue();
                        map.put("value", value);
                    }
                    if(i==2){
                        list1.add(map);
                        i=0;
                    }
                }
            }
            //System.out.println(list1);
        }
        return  list1;
    }

    @Override
    public int editSu(SystemUser systemUser) {
        return systemUserMapper.updateSu(systemUser);
    }

}
